package org.atovio.coolexam.service.impl;

import org.atovio.coolexam.entity.ExamPaperQuestion;
import org.atovio.coolexam.exception.ServiceException;
import org.atovio.coolexam.mapper.ExamPaperQuestionMapper;
import org.atovio.coolexam.service.ExamPaperQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ExamPaperQuestionServiceImpl implements ExamPaperQuestionService {

    private final ExamPaperQuestionMapper examPaperQuestionMapper;

    @Autowired
    public ExamPaperQuestionServiceImpl(ExamPaperQuestionMapper examPaperQuestionMapper) {
        this.examPaperQuestionMapper = examPaperQuestionMapper;
    }

    @Override
    public ExamPaperQuestion add(ExamPaperQuestion examPaperQuestion) {
        int count = examPaperQuestionMapper.insert(examPaperQuestion);
        if (count != 1)
            throw new ServiceException("数据库内部错误");

        return examPaperQuestionMapper.selectByExamPaperIdAndQuestionId(
                examPaperQuestion.getExamPaperId(), examPaperQuestion.getExamPaperId());
    }

    @Override
    public List<ExamPaperQuestion> getByExamPaperId(Integer examPaperId) {
        return examPaperQuestionMapper.selectByExamPaperId(examPaperId);
    }

    @Override
    public void deleteByExamPaperIdAndQuestionId(Integer examPaperId, Integer questionId) {
        int count = examPaperQuestionMapper.deleteByExamPaperIdAndQuestionId(
                examPaperId, questionId);
        if (count != 1)
            throw new ServiceException("数据库内部错误");
    }
}
